package io.renren.modules.skate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.skate.dao.FNewsDao;
import io.renren.modules.skate.dto.FNewsDTO;
import io.renren.modules.skate.entity.FNewsEntity;
import io.renren.modules.skate.service.FNewsService;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 新闻表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-11-28
 */
@Service
public class FNewsServiceImpl extends CrudServiceImpl<FNewsDao, FNewsEntity, FNewsDTO> implements FNewsService {

    @Override
    public QueryWrapper<FNewsEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<FNewsEntity> wrapper = new QueryWrapper<>();

        String type = (String) params.get("type");
        if (StringUtils.isNotEmpty(type)) {
            wrapper.eq("type", Integer.parseInt(type));
        }


        return wrapper;
    }


    @Override
    public List<FNewsDTO> getNewsByType(Integer classId, Integer type) {

        List<FNewsEntity> list = baseDao.selectList(new QueryWrapper<FNewsEntity>().eq("classify_id", classId).eq("type", type));
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日");
        list.forEach(t -> {
            String dateStr = sdf.format(t.getCreateDate());
            t.setCreateDateStr(dateStr);
        });

        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }

    /**
     * 获取教练列表
     *
     * @param type
     * @return
     */
    @Override
    public List<FNewsDTO> getCoachByType(Integer type) {
        List<FNewsEntity> list = baseDao.selectList(
                new QueryWrapper<FNewsEntity>()
                        .eq("type", type)
        );
        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }

    @Override
    public List<FNewsDTO> queryNewsList(Integer classId) {
        List<FNewsEntity> list = baseDao.selectList(
                new QueryWrapper<FNewsEntity>()
                        .eq("classify_id", classId)
        );
        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }
}